package RenRen;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.entity.mime.Header;

public class MyRenRenFriend {
	private static String userName = null;
	private static String password = null;
	private static HttpResponse response;
	private static DefaultHttpClient httpClient = null;

	public MyRenRenFriend(String userName, String password) {
		this.userName = userName;
		this.password = password;
		this.httpClient = new DefaultHttpClient();
	}

	public HttpResponse login(){
		String loginForm = "http://www.renren.com/PLogin.do";
		String origURL = "http://www.renren.com/Home.do";
		String domain = "renren.com";
		
		HttpResponse response = null;
		HttpPost httpPost = new HttpPost(loginForm);
		NameValuePair email = new BasicNameValuePair("email", userName);
		NameValuePair pass = new BasicNameValuePair("password", password);
		NameValuePair dom = new BasicNameValuePair("domain", domain);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		
		params.add(email);
		params.add(pass);
		params.add(dom);
		
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			response = httpClient.execute(httpPost);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			httpPost.abort();
		}
		return response;
//		String loginForm = "http://www.renren.com/PLogin.do";
//		String redirectURL = "http://www.renren.com/Home.do";
//		String domain = "renren.com";
//		
//		HttpResponse response = null;
//		HttpPost httpPost = new HttpPost(loginForm);
//		NameValuePair email = new BasicNameValuePair("email", userName);
//		NameValuePair pass = new BasicNameValuePair("password", password);
//		NameValuePair dom = new BasicNameValuePair("domain", domain);
//		NameValuePair origURL = new BasicNameValuePair("origURL", redirectURL);
//		NameValuePair autoLogin = new BasicNameValuePair("autoLogin", "true");
//		NameValuePair method = new BasicNameValuePair("method", "");
//		NameValuePair submit = new BasicNameValuePair("submit", "登录");
//		List<NameValuePair> params = new ArrayList<NameValuePair>();
//		
//		params.add(email);
//		params.add(pass);
//		params.add(dom);
//		params.add(origURL);
//		params.add(autoLogin);
//		params.add(method);
//		params.add(submit);
//		
//		try {
//			httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//			response = httpClient.execute(httpPost);
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally{
//			httpPost.abort();
//		}
//		return response;
	}
	
	public String getAllFriends(){
		String allFriendURL = "http://friend.renren.com/myfriendlistx.do";
		HttpGet httpGet = new HttpGet(allFriendURL);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		StringBuffer sb = null;
		try {
			sb = new StringBuffer(this.httpClient.execute(httpGet, responseHandler));
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			
			httpGet.abort();
		}
		return new String(sb);
	}
	
	private String getName(String str){
		StringBuffer sb = new StringBuffer(str);
		int pos;
		while ((pos = sb.indexOf("\\u")) > -1) {
			String tmp = sb.substring(pos, pos + 6);
			sb.replace(pos, pos + 6, Character.toString((char) Integer
					.parseInt(tmp.substring(2), 16)));
		}
		str = sb.toString();
		return str;
	}
	
	public void getFiendName(String str){
		String str1 = "var friends=[";
		String str2 = "var hotfriends=";
		String friend = str.substring(str.indexOf(str1), str.indexOf(str2));
		
		String regex = "name\":(.*?),\"head";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(friend.toString());
		while(matcher.find()){
			System.out.print(matcher.group(1));
//			System.out.println("----"+Test.getName(matcher.group(1)));
		}
	}
	
	public void gc(){

	}

	public static void main(String[] args) {
		MyRenRenFriend yww = new MyRenRenFriend("529595068@qq.com", "ZHANGFUYU1990");
		System.out.println(yww.login());
		String str = yww.getAllFriends();
		System.out.println("here:");
		System.out.println(str);
		System.out.println("Name");
		yww.getFiendName(str);
	}

}